import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/company_entity.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/button/normal_border_button.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

import 'default_bottom_sheet.dart';

/// 选择公司
///
/// onChangeCompany 更改公司
/// onCreateCompany 创建公司
/// onEditCompany 编辑公司
showCompanyBottomSheet(
  BuildContext context, {
  String companyId,
  List<CompanyEntity> companies,
  bool isScrollControlled = false,
  VoidCallback onCreateCompany,
  ValueChanged<CompanyEntity> onChangeCompany,
  ValueChanged<CompanyEntity> onEditCompany,
  ValueChanged<CompanyEntity> onInviteTeamMembers,
}) =>
    showDefaultBottomSheet(context,
        topMargin: 40,
        bottomMargin: 0,
        child: CompaniesWidget(
          companyId: companyId,
          companies: companies,
          onCreateCompany: onCreateCompany,
          onChangeCompany: onChangeCompany,
          onEditCompany: onEditCompany,
          onInviteTeamMembers: onInviteTeamMembers,
        ),
        isScrollControlled: true);

class CompaniesWidget extends StatelessWidget {
  final String companyId;
  final List<CompanyEntity> companies;
  final VoidCallback onCreateCompany;
  final ValueChanged<CompanyEntity> onChangeCompany;
  final ValueChanged<CompanyEntity> onEditCompany;
  final ValueChanged<CompanyEntity> onInviteTeamMembers;

  const CompaniesWidget({
    Key key,
    this.companyId,
    this.companies,
    this.onChangeCompany,
    this.onCreateCompany,
    this.onEditCompany,
    this.onInviteTeamMembers,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final maxHeight = MediaQuery.of(context).size.height * 2 / 3;
    return Container(
      constraints: BoxConstraints(
        maxHeight: maxHeight,
      ),
      child: ListView.builder(
        shrinkWrap: true,
        itemBuilder: _buildItems,
        itemCount: companies.length + 1,
      ),
    );
  }

  Widget _buildItems(BuildContext context, int index) {
    if (index == companies.length) {
      return PressedOpacityButton(
        onPressed: onCreateCompany,
        padding: EdgeInsets.symmetric(
          vertical: getDp(32),
          horizontal: getDp(41),
        ),
        border: Border(
          bottom: BorderSide(color: aceGrayColor, width: getDp(0.5)),
        ),
        child: Row(
          children: [
            Container(
              width: getDp(21),
              height: getDp(21),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: aceBlueColor,
              ),
              child: Image.asset(
                A.assetsImagesStyleAdd,
                width: getDp(12.5),
                height: getDp(12.5),
                fit: BoxFit.contain,
              ),
            ),
            SizedBox(
              width: getDp(30),
            ),
            Text(
              S.current.add_new_business,
              style: TextStyle(
                color: aceBlueColor,
                fontSize: getSp(18),
                fontWeight: FontWeight.w700,
                height: 16 / 18,
              ),
            ),
          ],
        ),
      );
    }
    final item = companies[index];
    bool isSelect = companyId == item.id.toString();
    return PressedOpacityButton(
      onPressed: () {
        onChangeCompany?.call(item);
      },
      child: Row(
        crossAxisAlignment: isSelect &&
                (Config.hasPermission(PermissionUtils.inviteTeamMembers) ||
                    Config.hasPermission(PermissionUtils.companySettings))
            ? CrossAxisAlignment.start
            : CrossAxisAlignment.center,
        children: [
          CircleAvatar(
            radius: getDp(22),
            backgroundImage: Config.toImageProvider(item.logo),
            backgroundColor: aceGrayColor,
          ),
          SizedBox(
            width: getDp(20),
          ),
          Expanded(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      child: Text(
                        item.name,
                        style: TextStyle(
                          color: isSelect ? aceBlueColor : aceBlackColor,
                          fontSize: getSp(18),
                          fontWeight:
                              isSelect ? FontWeight.w700 : FontWeight.w400,
                          height: 18 / 16,
                        ),
                      ),
                      constraints: BoxConstraints(
                        maxWidth: getDp(200),
                      ),
                    ),
                    if (isSelect)
                      Padding(
                        padding: EdgeInsets.only(left: getDp(13)),
                        child: SvgPicture.asset(
                          A.assetsImagesInvitationSelected,
                          width: getDp(20),
                          height: getDp(20),
                        ),
                      ),
                  ],
                ),
                if (isSelect &&
                    Config.hasPermission(PermissionUtils.companySettings))
                  Padding(
                    padding: EdgeInsets.only(top: getDp(10)),
                    child: SizedBox(
                      width: 150,
                      child: NormalBorderButton(
                        title: S.current.edit_business_profile,
                        backgroundColor: aceBlueColor,
                        textColor: Colors.white,
                        onTap: () {
                          onEditCompany?.call(item);
                        },
                      ),
                    ),
                  ),
                if (isSelect &&
                    Config.hasPermission(PermissionUtils.inviteTeamMembers))
                  Padding(
                    padding: EdgeInsets.only(top: getDp(10)),
                    child: SizedBox(
                      width: 150,
                      child: NormalBorderButton(
                        title: S.current.manage_users,
                        backgroundColor: aceBlueColor,
                        textColor: Colors.white,
                        onTap: () {
                          onInviteTeamMembers?.call(item);
                        },
                      ),
                    ),
                  ),
              ],
            ),
          )
        ],
      ),
      padding: EdgeInsets.symmetric(
        horizontal: getDp(25),
        vertical: getDp(20),
      ),
      border: Border(
        bottom: BorderSide(color: aceGrayColor, width: getDp(0.5)),
      ),
    );
  }
}
